package com.haifeng.lyw.jinganghc.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.haifeng.lyw.jinganghc.R;
import com.haifeng.lyw.jinganghc.adapter.UserAdapter;
import com.haifeng.lyw.jinganghc.bluetooth.WorkView;
import com.haifeng.lyw.jinganghc.dao.UserDao;
import com.haifeng.lyw.jinganghc.dao.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class SetActivity extends Activity implements View.OnClickListener{
    private RecyclerView recyclerView;
    private List<UserInfo> lists = new ArrayList<>();
    private UserAdapter adapter;
    private ImageView iv_goback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        initView();

    }

    private void initView() {
        iv_goback=findViewById(R.id.iv_goback);
        iv_goback.setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        adapter = new UserAdapter(SetActivity.this, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(SetActivity.this));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position) {
//                String pos = String.valueOf(position);

                AlertDialog.Builder builder = new AlertDialog.Builder(SetActivity.this);
                // builder.setIcon(R.drawable.ic_launcher);

                // 指定下拉列表的显示数据
                final String[] memu = {"修改", "取消"};
                // 设置一个下拉的列表选择项
                builder.setItems(memu, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                try {


                                    Intent toAdd = new Intent(SetActivity.this, UpActivity.class);

                                    toAdd.putExtra("position",position);

                                    startActivity(toAdd);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                    // TODO: handle exception
                                }



                                break;
                            case 1:
                                // Toast.makeText(context, "选择：" + memu[which],
                                // Toast.LENGTH_SHORT).show();
                                try {

                                } catch (Exception e) {
                                    e.printStackTrace();
                                    // TODO: handle exception
                                }
                                break;
                            default:
                                break;
                        }

                    }
                });
                builder.show();
            }
        });
    }

    /**
     * 查询数据
     */
    private void queryListData() {
        lists = UserDao.getInstance().queryAllData();
        adapter.setData(lists);
        //  Toast.makeText(getActivity(), "查询到" + lists.size() + "条数据", Toast.LENGTH_SHORT).show();

    }




    //update sql data
//    private void upDateSQLite(long id, String name, String sex) {
//        userDao.update(new User(id, name, sex));
//    }
    @Override
    public void onResume() {
        super.onResume();
        queryListData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_goback:
                Intent toWork =new Intent(SetActivity.this, WorkView.class);
                startActivity(toWork);
                finish();
                break;
        }
    }
}
